<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\admin\model\charge\Place;
use app\admin\model\cabinet\Info;
use app\admin\model\cabinet\Lending;
use app\admin\model\cabinet\Type;


/**
 * 充电站相关接口
 */
class Charge extends Api
{
    protected $noNeedLogin = ['getAroundChargePlace'];
    protected $noNeedRight = '*';
    protected $relationSearch = true;

    /**
     * 获取附近的充电站
     *
     * @summary 获取地图所见范围内所有充电站信息
     * @method GET
     * @param string $northEast 东北方向经纬度
     * @param string $northEast 西南方向经纬度
     */
    public function getAroundChargePlace()
    {
        $ChargePlace = new Place();
        $CabinetInfo = new Info();
        $CabinetLending = new Lending();
        $CabinetType = new Type();

        $northEast = explode(',', $this->request->get('northEast'));//[经度，纬度]
        $southWest = explode(',', $this->request->get('southWest'));//[经度，纬度]
        if ($northEast[1] < $southWest[1]) {
            $latMin = $northEast[1];
            $latMax = $southWest[1];
        } else {
            $latMin = $southWest[1];
            $latMax = $northEast[1];
        }
        if ($northEast[0] < $southWest[0]) {
            $lngMin = $northEast[0];
            $lngMax = $southWest[0];
        } else {
            $lngMin = $southWest[0];
            $lngMax = $northEast[0];
        }

        $chargePlaceList = $ChargePlace->with(['type'])
            ->where('place_lat>=' . $latMin . ' AND place_lat<=' . $latMax . ' AND place_lng >=' . $lngMin . ' AND place_lng<=' . $lngMax . ' AND status = "1"')
            ->select();

        foreach ($chargePlaceList as $chargePlaceIndex => $chargePlace) {
            $slotNum = 0;
            $cabinetIdArray = $CabinetInfo
                ->where('charge_place_id=' . $chargePlace['id'] . ' And status = "2" AND running_status = "1"')
                ->column('id');
            $cabinetList = $CabinetInfo
                ->where('charge_place_id=' . $chargePlace['id'] . ' And status = "2" AND running_status = "1"')
                ->select();
            foreach ($cabinetList as $cabinet) {
                $slotNum = $slotNum + $CabinetType->where('id=' . $cabinet['cabinet_type_id'])->value('slot_number');
            }
            $cabinetAndroidSum = 0;
            $cabinetIosSum = 0;
            $cabinetTypeCSum = 0;
            if (count($cabinetIdArray) > 0) {
                $cabinetAndroidSum = $CabinetLending
                    ->where('cabinet_info_id in (' . implode(',', $cabinetIdArray) . ') AND status = "1" AND runningdata = "0" AND support = "1"')
                    ->count();
                $cabinetIosSum = $CabinetLending
                    ->where('cabinet_info_id in (' . implode(',', $cabinetIdArray) . ') AND status = "1" AND runningdata = "0" AND support = "2"')
                    ->count();
                $cabinetTypeCSum = $CabinetLending
                    ->where('cabinet_info_id in (' . implode(',', $cabinetIdArray) . ') AND status = "1" AND runningdata = "0" AND support = "3"')
                    ->count();
            }
            $useNum = $cabinetAndroidSum + $cabinetIosSum + $cabinetTypeCSum;

            $chargePlaceList[$chargePlaceIndex]['cabinetAndroidSum'] = $cabinetAndroidSum;
            $chargePlaceList[$chargePlaceIndex]['cabinetIosSum'] = $cabinetIosSum;
            $chargePlaceList[$chargePlaceIndex]['cabinetTypeCSum'] = $cabinetTypeCSum;
            $chargePlaceList[$chargePlaceIndex]['cabinetIdList'] = $cabinetList;
            $chargePlaceList[$chargePlaceIndex]['slotNum'] = $slotNum;
            $chargePlaceList[$chargePlaceIndex]['useNum'] = $useNum;


        }

        $this->success('返回成功', $chargePlaceList);
    }

    /**
     * 获取充电机柜相关信息
     *
     * @summary 通过机柜编号获取机柜设备信息
     * @method GET
     * @param string $cabinetName
     */
    public function getCabinetByName($cabinetName)
    {
        $CabinetInfo = new Info();
        $CabinetLending = new Lending();

        $cabinetInfo = $CabinetInfo->alias('info')
            ->with(['type', 'place'])
            ->where('info.name', $cabinetName)
            ->find();

        $cabinetLendingList = $CabinetLending
            ->where('cabinet_info_id' , $cabinetInfo['id'])
            ->where('status', '1')
            ->where('runningdata','0')
            ->select();


        $this->success('返回成功', array('cabinetInfo' => $cabinetInfo, 'cabinetLendingList' => $cabinetLendingList));
    }

}